package com.briup.prs;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.lib.db.DBWritable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RecommendShop implements DBWritable, WritableComparable<RecommendShop> {
    private long user;
    private  long shop;
    private double  recommendValue;

    @Override
    public int compareTo(RecommendShop o) {
        return (int)(this.recommendValue-o.recommendValue);
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(user);
        out.writeLong(shop);
        out.writeDouble(recommendValue);

    }

    @Override
    public void readFields(DataInput in) throws IOException {
        user=in.readLong();
        shop=in.readLong();
        recommendValue=in.readDouble();

    }

    @Override
//    insert into tableName(colName,colName,colName) values(?,?,?)
    public void write(PreparedStatement statement) throws SQLException {
        statement.setLong(1,user);
        statement.setLong(2,shop);
        statement.setDouble(3,recommendValue);
    }
    @Override
    public void readFields(ResultSet resultSet) throws SQLException {
        user=resultSet.getLong(1);
        shop=resultSet.getLong(2);
        recommendValue=resultSet.getDouble(3);
    }
}
